package com.example.tijian_doctor_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.tijian_doctor_backend.dto.OrdersPageResponseDTO;
import com.example.tijian_doctor_backend.dto.SelectForm;
import com.example.tijian_doctor_backend.pojo.Orders;
import com.example.tijian_doctor_backend.pojo.Setmeal;
import com.example.tijian_doctor_backend.pojo.Users;
import com.example.tijian_doctor_backend.result.Result;
import com.example.tijian_doctor_backend.service.OrdersService;
import com.example.tijian_doctor_backend.service.SetmealService;
import com.example.tijian_doctor_backend.service.UsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.statement.select.Offset;
import org.apache.tomcat.jni.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: bong~
 * @description: TODO
 * @date: 2024/6/20 14:09
 * @version: 1.0
 */

@RestController
@Slf4j
@RequestMapping("/orders")
@Api(tags ="订单接口")
@CrossOrigin("*")
public class OrdersController {

    @Autowired
    private UsersService usersService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private OrdersService ordersService;

    @ApiOperation("根据个人信息查询")
    @PostMapping("/listOrders")
    public Result listOrdersPersonal(@RequestBody SelectForm selectForm){

//        String userId=selectForm.getUserId();
//        String realName=selectForm.getRealName();
//        Integer sex=selectForm.getSex();
//
//
//        QueryWrapper<Users> queryWrapper1=new QueryWrapper<Users>()
//                .eq("userId",userId)
//                .eq("realName",realName)
//                .eq("sex",sex);
//        Users cur=usersService.getOne(queryWrapper1);
//
//        System.out.println(cur);
//        if(cur==null){
//            return Result.error("用户信息错误");
//        }

        System.out.println(selectForm.toString());
        OrdersPageResponseDTO dto=ordersService.selectOrderByForm(selectForm);


        return Result.success(dto);










    }


    @ApiOperation("根据订单消息查询用户和套餐信息")
    @PostMapping("/getOrdersById")
    public Result getOrdersById(@RequestParam Integer orderId){
//        System.out.println(orderId);

        Orders orders = ordersService.getById(orderId);

        QueryWrapper<Users> queryWrapper1=new QueryWrapper<Users>()
                .eq("userId",orders.getUserId());
        Users users= usersService.getOne(queryWrapper1);

        QueryWrapper<Setmeal> queryWrapper2=new QueryWrapper<Setmeal>()
                .eq("smId",orders.getSmId());
        Setmeal setmeal=setmealService.getOne(queryWrapper2);

        Map<String,Object> map=new HashMap<String,Object>();

        System.out.println(orders.getOrderDate());

        map.put("state",orders.getState());
        map.put("orderId",orders.getOrderId());
        map.put("userId",orders.getUserId());
        map.put("orderDate",orders.getOrderDate().toString());
        map.put("users",users);
        map.put("setmeal",setmeal);

        return Result.success(map);


    }

    @ApiOperation("归档总检报告")
    @PostMapping("/updateOrdersState")
    public Result updateOrdersState(@RequestParam Integer orderId,@RequestParam Integer state){

        UpdateWrapper<Orders> updateWrapper=new UpdateWrapper<Orders>()
                .eq("orderId",orderId);
        Orders orders=new Orders();
        orders.setState(state);

        ordersService.update(orders,updateWrapper);

        return Result.success();
    }
}
